package by.apiskun.curex.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.apiskun.curex.dataaccess.MoneyDAO;
import by.apiskun.curex.datamodel.Money;
import by.apiskun.curex.services.MoneyService;

@Service
public class MoneyServiceImpl implements MoneyService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(MoneyServiceImpl.class);

	@Inject
	private MoneyDAO moneyDAO;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of ProductService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public Money getId(Long id) {
		Money entity = moneyDAO.getById(id);

		return entity;
	}

	@Override
	public void saveOrUpdate(Money money) {
		if (money.getId() == null) {
			LOGGER.debug("Save new: {}", money);
			moneyDAO.insert(money);
		} else {
			LOGGER.debug("Update: {}", money);
			moneyDAO.update(money);
		}

	}

	@Override
	public void delete(Money money) {
		LOGGER.debug("Remove: {}", money);
		moneyDAO.delete(money.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all money");
		moneyDAO.deleteAll();

	}

	@Override
	public Long getCount() {
		Long count = moneyDAO.getCount();
		return count;
	}

	@Override
	public List<Money> getAllMoney() {
		List<Money> money = moneyDAO.getAllMoney();
		return money;
	}

	@Override
	public List<Money> getAllMoneyByName(String name) {
		List<Money> money = moneyDAO.getAllMoneyByName(name);
		return money;
	}

	@Override
	public void updateMoneyName(Long id, String newName) {
		moneyDAO.updateMoneyName(id, newName);

	}

}
